package org.example.securitydemo.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Controller
public class IndexController {
    @ResponseBody
    @GetMapping("/")
    public Map<String,Object> index() {
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        Object principal = authentication.getPrincipal();
        Object credentials = authentication.getCredentials();// 这里的密码要脱敏处理
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        String name = authentication.getName();
        Map<String,Object> map = new HashMap<>();
        map.put("username",name);
        map.put("credentials",credentials);
        return map;
    }

    @GetMapping("dm")
    public String dm() {
        return "dm";
    }
}
